////////////////////////////////////////////////////////
//  constructor of SubTitleBar
SubTitleBar = function () {
    this.top = new Array();
    this.videoCenter = null;
    this.oldSubtitle = -1;
    this.length = 0;
};

//  methods of SubTitleBar
SubTitleBar.prototype = {
    init: function (id) {
        this.bar = document.getElementById(id);
    },
    onItemClick: function (time) {
        this.videoCenter.onSeekEvent(time);
    },
    registerVideoCenter: function (videoCenter) {
        this.videoCenter = videoCenter;
    },
    scroll: function (time) {
		
        if (time < this.subtitleArray[0].startTime) {		//还没到字幕时间
            if (this.oldSubtitle != -1)
                document.getElementById("subtitle" + this.oldSubtitle).style.fontWeight = "normal";
            return;
        }

        if (time >= this.subtitleArray[this.length - 1].endTime) {		//字幕结束
            if (this.oldSubtitle != -1)
                document.getElementById("subtitle" + this.oldSubtitle).style.fontWeight = "normal";
            return;
        }

        if (this.oldSubtitle != -1) 		//还处于当前字幕
            if (time >= this.subtitleArray[this.oldSubtitle].startTime && time < this.subtitleArray[this.oldSubtitle].endTime)
                return;

        if (this.oldSubtitle != -1 && this.oldSubtitle < this.length - 1)			//处于当前字幕与下一字幕之间
            if (time >= this.subtitleArray[this.oldSubtitle].endTime && time < this.subtitleArray[this.oldSubtitle + 1].stratTime)
                return;

        if (this.oldSubtitle != -1)			//正处于下一个字幕
            if (time >= this.subtitleArray[this.oldSubtitle + 1].startTime && time < this.subtitleArray[this.oldSubtitle + 1].endTime) {
                this.bar.scrollTop = this.top[this.oldSubtitle + 1];
                document.getElementById("subtitle" + this.oldSubtitle).style.fontWeight = "normal";
                this.oldSubtitle++;
                document.getElementById("subtitle" + this.oldSubtitle).style.fontWeight = "bold";
                return;
            }

        for (var i = 0; i < this.length; i++)		//寻找当前处于的字幕
            if (time >= this.subtitleArray[i].startTime && time < this.subtitleArray[i].endTime) {
                this.bar.scrollTop = this.top[i];
                if (this.oldSubtitle != -1)
                    document.getElementById("subtitle" + this.oldSubtitle).style.fontWeight = "normal";
                this.oldSubtitle = i;
                document.getElementById("subtitle" + i).style.fontWeight = "bold";
                break;
            }
    },

    display: function (subtitleArray) {
        this.subtitleArray = subtitleArray;
        this.length = subtitleArray.length;
        var totalSubtitle = 0;
        var minSubtitle = 5120;
        for (var i = 0; i < this.length; i++) {
            this.bar.innerHTML += "<li id=\"subtitle" + i + "\" onclick=\"onSubTitleBarClicked(" + this.subtitleArray[i].startTime + ")\">" + this.subtitleArray[i].subtitleString + "</li>";
            var temp = document.getElementById("subtitle" + i).scrollHeight;
            if (temp < minSubtitle) minSubtitle = temp;
            totalSubtitle += temp;			//字幕总高
        }
        var height = this.bar.scrollHeight;			//subtitleBar组件总高
        var gap = parseInt((height - totalSubtitle) / this.length);		//字幕间的间隙
        var visualHeight = this.bar.clientHeight;
        for (var i = 0; i < parseInt(visualHeight / (2 * (minSubtitle + gap)))/2 ; i++)
            this.bar.innerHTML = "<li>&nbsp</li>" + this.bar.innerHTML + "<li>&nbsp</li>";
        this.top[0] = 0;
        for (var i = 1; i < this.length; i++) {
            this.top[i] = this.top[i - 1] + gap + document.getElementById("subtitle" + (i - 1)).scrollHeight;
        }
        this.bar.scrollTop = 0;
    },
};